package com.pzl.treeview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.pzl.library.SimpleNode;
import com.pzl.library.SimpleRoot;

/**
 * Created by zl.peng on 2016-03-25.
 */
public class SimpleTreeAdapter extends BaseAdapter {
    private static final String TAG = "SimpleTreeAdapter";
    private Context context;
    private SimpleRoot simpleRoot;

    public SimpleTreeAdapter(SimpleRoot simpleRoot, Context context) {
        this.context = context;
        this.simpleRoot = simpleRoot;
        this.simpleRoot.init();
    }

    @Override
    public int getCount() {
        return simpleRoot.getNodeList().size();
    }

    @Override
    public Object getItem(int position) {
        return simpleRoot.getNodeList().get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final SimpleNode simpleNode = (SimpleNode) simpleRoot.getNodeList().get(position);
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_simple, null);
            viewHolder = new ViewHolder();
            viewHolder.iv = (ImageView) convertView.findViewById(R.id.iv);
            viewHolder.cb = (CheckBox) convertView.findViewById(R.id.cb);
            viewHolder.tv = (TextView) convertView.findViewById(R.id.tv);
            convertView.setTag(viewHolder);
        }
        viewHolder = (ViewHolder) convertView.getTag();
        viewHolder.iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                simpleNode.setExpand(!simpleNode.isExpand());
                notifyDataSetChanged();
            }
        });
        if (!simpleNode.isLeaf() && simpleNode.isExpand()) {
            viewHolder.iv.setVisibility(View.VISIBLE);
            viewHolder.iv.setImageResource(R.drawable.arrow_down);
        } else if (!simpleNode.isLeaf() && !simpleNode.isExpand()) {
            viewHolder.iv.setVisibility(View.VISIBLE);
            viewHolder.iv.setImageResource(R.drawable.arrow_right);
        } else {
            viewHolder.iv.setVisibility(View.INVISIBLE);
        }
        /**
         * CompoundButton.OncheckedChangeListener,先更新listener，后setChecked
         */
        viewHolder.cb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean checkFlag = !simpleNode.isSelected();
                onNodeChecked(simpleNode, checkFlag);
                simpleRoot.printNodeList();
                notifyDataSetChanged();
            }
        });
        viewHolder.cb.setChecked(simpleNode.isSelected());
        viewHolder.tv.setText(simpleNode.getText());
        convertView.setPadding(70 * simpleNode.getLevel(), 0, 0, 0);
        return convertView;
    }

    @Override
    public void notifyDataSetChanged() {
        simpleRoot.update();
        super.notifyDataSetChanged();
    }

    /**
     * 勾选节点
     */
    private void onNodeChecked(SimpleNode simpleNode, boolean checkFlag) {
        if (!simpleNode.isLeaf()) {//非叶子节点
            for (int i = 0; i < simpleNode.getChildren().size(); i++) {
                SimpleNode childNode = (SimpleNode) simpleNode.getChildren().get(i);
                onNodeChecked(childNode, checkFlag);
            }
            simpleNode.setSelected(checkFlag);
        } else {//叶子节点
            simpleNode.setSelected(checkFlag);
        }
    }

    static class ViewHolder {
        private ImageView iv;
        private CheckBox cb;
        private TextView tv;
    }
}
